using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JwtAuthDemo.Helpers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using webapi.Domain.Repository;
using webapi.Infrastructure.Db;
using webapi.Infrastructure.Filters;
using webapi.Infrastructure.Repository;
using Webapi.Domain.Entity;

namespace webapi.api
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var Sqlserver = Configuration.GetConnectionString("Sqlserver");
            services.AddDbContext<webapiDbContext>(options => options.UseSqlServer(Sqlserver));

            // 实现CRUD的依赖注入
            services.AddScoped(typeof(IRepository<>), typeof(EFRepository<>));

            // 注册service filter 
            // services.AddSingleton<ExecetionFilters>();
            services.AddScoped<ExceptionFilter>();

            // token注入
            services.AddSingleton<JwtHelpers>();
            services.AddSwaggerGen(c =>
            {
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
                {

                    Description = "在下框中输入请求头中需要添加Jwt授权Token：Bearer Token",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey,
                    BearerFormat = "JWT",
                    Scheme = "Bearer"
                });

                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference {
                                Type = ReferenceType.SecurityScheme,
                                Id = "Bearer"
                            }
                        },
                        new string[] { }
                    }
                });
            });
            // 注入异常过滤器
            services.AddControllers(options =>
            {
                options.Filters.Add(typeof(ExceptionFilter)); // 异常过滤器注入
                options.Filters.Add(typeof(AuditLogFilter)); // 审计注入
            });
            // services.AddControllers(options=>options.Filters.Add(new ExceptionFilter()));

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
